﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using ConexionCliente.Responses;

namespace Cliente
{
    public partial class ClientForm : Form
    {
        private RequestManager requestService = null;
        private Config conf = null;

        public ClientForm()
        {
            InitializeComponent();

            btnCpu.Enabled = false;
            btnDisco.Enabled = false;
            btnRam.Enabled = false;

            //  Carga la configuracion del XML del Cliente
            conf = new Config(""); //  

            // Inicio el servicio para enviar comandos a los servidiores
            requestService = new RequestManager(conf, this);

        }
        
        private void btnCpu_Click(object sender, EventArgs e)
        {
            ControlesCPU(true);
            requestService.ConsultarCPUManager();
        }

        private void btnDisco_Click(object sender, EventArgs e)
        {
            ControlesDisco(true);
            requestService.ConsultarDiscoManager();
        }

        private void btnRam_Click(object sender, EventArgs e)
        {
            ControlesRam(true);
            requestService.ConsultarMemoriaManager();
        }

        private void btnActualizar_Click(object sender, EventArgs e)
        {
           int cant =  requestService.ConsultarServidoresManager();
           if (cant == 0)
           {
               btnCpu.Enabled = false;
               btnDisco.Enabled = false;
               btnRam.Enabled = false;
           }
           else
           {
               btnCpu.Enabled = true;
               btnDisco.Enabled = true;
               btnRam.Enabled = true;
           }

        }

        #region Mostrar/Ocultar Controles
        private void ControlesRam(bool visible)
        {
            if (visible)
            {
                ControlesCPU(false);
                ControlesDisco(false);
            }
            lblExcepcion.Text = "";
            lblResRamLibre.Visible = visible;
            lblResRamTotal.Visible = visible;
            lblRamLibre.Visible = visible;
            lblRamTotal.Visible = visible;
            picRam.Visible = visible;
        }
        private void ControlesCPU(bool visible)
        {
            if (visible)
            {
                ControlesRam(false);
                ControlesDisco(false);
            }
            lblExcepcion.Text = "";
            lblCpuUsage.Visible = visible;
            lblResCpu.Visible = visible;
            picCPU.Visible = visible;
        }
        private void ControlesDisco(bool visible)
        {
            if (visible)
            {
                ControlesCPU(false);
                ControlesRam(false);
            }
            lblExcepcion.Text = "";
            lblResDiscoLibre.Visible = visible;
            lblResDiscoTotal.Visible = visible;
            lblDiscoLibre.Visible = visible;
            lblDiscoTotal.Visible = visible;
            picDisco.Visible = visible;
        }
        #endregion

        public void setRam(String total, String libres){
            lblRamLibre.Text = "Memoria Libre: " + libres + " Mb";
            lblRamTotal.Text = "Memoria Total: " + total + " Mb";
        }

        public void setCpu(String cpu)
        {
            lblCpuUsage.Text = "Uso CPU: " + cpu + " %";
        }

        public void setDisco(String total, String libres)
        {
            lblDiscoLibre.Text = "Disco Libre: " + libres + " GB";
            lblDiscoTotal.Text = "Disco Total: " + total + " GB";
        }

        public ListBox getListBox()
        {
            return lstServers;
        }

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {          
            Application.Exit();
        }        
    }
}
